package com.example.haohao.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

/**
 *
 */
public class SwitchThree extends LinearLayout implements View.OnClickListener {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    switchClick clickListener;
    private LinearLayout ll_switch;
    private Paint mPaint;

    public SwitchThree(Context context) {
        super(context);
        init(context);
    }


    public SwitchThree(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SwitchThree(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.ll_switch_layout, this);
        ll_switch = (LinearLayout) findViewById(R.id.ll_switch);
        tv1 = (TextView) findViewById(R.id.ll_switch_tv1);
        tv2 = (TextView) findViewById(R.id.ll_switch_tv2);
        tv3 = (TextView) findViewById(R.id.ll_switch_tv3);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        setSelected(0);
    }


    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.ll_switch_tv1) {
            setSelected(0);
            clickListener.onSwitch3Title1Clicked();
        } else if (viewId == R.id.ll_switch_tv2) {
            setSelected(1);
            clickListener.onSwitch3Title2Clicked();
        } else if (viewId == R.id.ll_switch_tv3) {
            setSelected(2);
            clickListener.onSwitch3Title3Clicked();
        }
    }


    /**
     * @param titls 参数为长度为3的字符串数组
     */
    public void setTitles(String[] titls) {
        if (titls != null && titls.length == 3) {
            tv1.setText(titls[0]);
            tv2.setText(titls[1]);
            tv3.setText(titls[2]);
        } else {
            Toast.makeText(getContext(), "SwitchThree:请输入长度为3的字符串数组", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * @param position
     * 设置选中的下标，从0开始
     */
    public void setSelected(int position) {
        switch (position) {
            case 0:
                tv1.setBackgroundResource(R.drawable.shape_frame_view_left_red);
                tv2.setBackgroundColor(Color.WHITE);
                tv3.setBackgroundResource(R.drawable.shape_frame_view_right_white);
                tv1.setTextColor(Color.WHITE);
                tv2.setTextColor(Color.RED);
                tv3.setTextColor(Color.RED);
                break;
            case 1:
                tv1.setBackgroundResource(R.drawable.shape_frame_view_left_white);
                tv2.setBackgroundColor(Color.parseColor("#FF334E"));
                tv3.setBackgroundResource(R.drawable.shape_frame_view_right_white);
                tv1.setTextColor(Color.RED);
                tv2.setTextColor(Color.WHITE);
                tv3.setTextColor(Color.RED);
                break;
            case 2:
                tv1.setBackgroundResource(R.drawable.shape_frame_view_left_white);
                tv2.setBackgroundColor(Color.WHITE);
                tv3.setBackgroundResource(R.drawable.shape_frame_view_right_red);
                tv1.setTextColor(Color.RED);
                tv2.setTextColor(Color.RED);
                tv3.setTextColor(Color.WHITE);
                break;
        }
    }

    public void setOnclickListener(switchClick click) {
        clickListener = click;
    }

    interface switchClick {
        void onSwitch3Title1Clicked();

        void onSwitch3Title2Clicked();

        void onSwitch3Title3Clicked();
    }
}
